//
//  UILabel+JKExtension.m
//  Category
//
//  Created by jamalping on 16/4/7.
//  Copyright © 2016年 jamalping. All rights reserved.
//

#import "UILabel+JKExtension.h"

@implementation UILabel (JKExtension)

// 计算文本高度
+ (CGFloat)calculateTextHeight:(NSString *)labalText textWidth:(CGFloat)defaultWidth textFont:(CGFloat)font {
    NSInteger hh;
    UIFont *vFont;
    if (font==0.0) {
        vFont = [UIFont systemFontOfSize:15];
    }else {
        vFont = [UIFont systemFontOfSize:font];
    }
    if ([[[UIDevice currentDevice] systemVersion] floatValue]>=7.0) {
        // 自适应
        CGRect rectq = [labalText boundingRectWithSize:CGSizeMake(defaultWidth, NSIntegerMax)
                                               options:NSStringDrawingUsesLineFragmentOrigin
                                            attributes:@{vFont:NSFontAttributeName}
                                               context:nil];
        hh = rectq.size.height;
    }else {
        hh = [labalText sizeWithFont:vFont
                   constrainedToSize:CGSizeMake(defaultWidth,NSIntegerMax)].height;
    }
    return hh+20;
}

// 计算文本宽度
+ (CGSize)calculateTextWidth:(NSString *)labalText textFont:(UIFont *)font {
    CGSize size;
    UIFont *vFont;
    if (font==nil) {
        vFont = [UIFont systemFontOfSize:15];
    }else {
        vFont = font;
    }
    if ([[[UIDevice currentDevice] systemVersion] floatValue]>=7.0) {
        // 自适应
        size = [labalText sizeWithAttributes:@{vFont:NSFontAttributeName}];
    }else {
        size = [labalText sizeWithFont:vFont];
    }
    return size;
}

@end
